//
//  CMJTabbarButton.m
//  CMJWeibo
//
//  Created by BH on 15/12/3.
//
//

#import "CMJTabbarButton.h"
#import "CMJBadgeButton.h"
#import "UIView+MJ.h"

#define CMJTabBarButtonImageRatio 0.6  //按钮内image占整个按钮比例

@interface CMJTabbarButton ()

@property(nonatomic,weak)CMJBadgeButton *badgeBtn;

@end

@implementation CMJTabbarButton

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //图片居中
        self.imageView.contentMode = UIViewContentModeCenter;
        
        //文字居中、大小、颜色
        self.titleLabel.font = [UIFont systemFontOfSize:11.f];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        
        //添加badgeButton
        CMJBadgeButton *button = [[CMJBadgeButton alloc] init];
        button.hidden = YES;
        [self addSubview:button];
        self.badgeBtn = button;
    }
    return self;
}

// 内部图片的frame
-(CGRect)imageRectForContentRect:(CGRect)contentRect
{
    CGFloat imageW = self.width;
    CGFloat imageH = self.height * CMJTabBarButtonImageRatio;
    return CGRectMake(0, 0, imageW, imageH);
}

// 内部文字的frame
-(CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGFloat titleY = self.height * CMJTabBarButtonImageRatio;
    CGFloat titleW = self.width;
    CGFloat titleH = self.height - titleY;
    return CGRectMake(0, titleY, titleW, titleH);
}

//  设置选项卡
-(void)setItem:(UITabBarItem *)item
{
    _item = item;
    //文字
    [self setTitle:item.title forState:UIControlStateNormal];
    //图标
    [self setImage:item.image forState:UIControlStateNormal];
    //选中的图标
    [self setImage:item.selectedImage forState:UIControlStateSelected];
    
    //self监听item的4个属性值的改变
    [item addObserver:self forKeyPath:@"badgevalue" options:0 context:nil];
    [item addObserver:self forKeyPath:@"title" options:0 context:nil];
    [item addObserver:self forKeyPath:@"image" options:0 context:nil];
    [item addObserver:self forKeyPath:@"selectedimage" options:0 context:nil];
    
}

/**
 *  当利用KVO监听到某个对象的属性改变了, 就会调用这个方法
 *
 *  @param keyPath 被改变的属性的名称
 *  @param object  被监听的那个对象
 *  @param change  存放者被改变属性的值
 */
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    //设置badgeButton
    self.badgeBtn.badgeValue = self.item.badgeValue;
    
    //设置按钮文字
    [self setTitle:self.item.title forState:UIControlStateNormal];
    [self setTitle:self.item.title forState:UIControlStateSelected];
    
    //设置按钮图片
    [self setImage:self.item.image forState:UIControlStateNormal];
    [self setImage:self.item.selectedImage forState:UIControlStateSelected];
}

// 设置提醒数字的位置和尺寸
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.badgeBtn.x = self.width - self.badgeBtn.width - 10;
    self.badgeBtn.y = 0;
}

#warning 一定要移除监听器
-(void)dealloc
{
    [self.item removeObserver:self forKeyPath:@"badgevalue"];
    [self.item removeObserver:self forKeyPath:@"title"];
    [self.item removeObserver:self forKeyPath:@"image"];
    [self.item removeObserver:self forKeyPath:@"selectedimage"];
}

@end
